<?php
namespace data\model;

use think\model\concern\SoftDelete;

class Article extends BaseModel{
    /**
     * 文章类型
     */
    const ARTICLE_TYPE_TECH = 1;
    const ARTICLE_TYPE_LIFE = 2;

    use SoftDelete;
    protected $deleteTime = 'delete_time';

    protected $autoWriteTimestamp = true;

    protected $auto = ['author','option'];

    /**
     * 获取文章类型名
     * @return array
     */
    public static function getArticleType(){
        return [
            self::ARTICLE_TYPE_TECH => '技术博客',
            self::ARTICLE_TYPE_LIFE=>'生活日记'
        ];
    }

    /**
     * 获取文章类型的英文名
     */
    public static function getArticleTypeOfUs(){
        return [
            self::ARTICLE_TYPE_TECH => 'technology',
            self::ARTICLE_TYPE_LIFE=>'dailyLife'
        ];
    }

    protected function setAuthorAttr(){
        $user = session('user');
        return $user->nickname;
    }

    protected function setOptionAttr($value){
        return $value ? $value : json_encode(array_keys(input('param.')['options']));
    }

    public function getLabelTextAttr($value,$data)
    {
        $label = new Label;
        return $label->getById($data['label_id'])['name'];
    }

    /**
     * 判断文章是否允许显示
     */
    public function isShow(){
        return strpos($this->getAttr('option'),'show');
    }

    /**
     * 判断文章是否允许置顶
     */
    public function isTop(){
        return strpos($this->getAttr('option'),'top');
    }

    /**
     * 判断文章是否允许推荐
     */
    public function isRecommend(){
        return strpos($this->getAttr('option'),'recommend');
    }


}